package it.gat.webservice.resource.impl;

import java.sql.SQLException;

import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import it.gat.dao.RapportoInterventoDAO;
import it.gat.model.RapportoIntervento;
import it.gat.util.dao.DAOFactory;
import it.gat.webservice.utils.Impostazioni;

public class RapportoInterventoResource extends ServerResource{
	
	@Post
	public RapportoIntervento insert(RapportoIntervento rapportoIntervento) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		RapportoInterventoDAO rapportoInterventoDAO = daoFactory.getRapportoInterventoDAO();
		RapportoIntervento nuovoRapportoIntervento = null;
		try {
			nuovoRapportoIntervento = rapportoInterventoDAO.insert(rapportoIntervento);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return nuovoRapportoIntervento;
	}

	@Get
	public RapportoIntervento retriveById() {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		RapportoInterventoDAO rapportoInterventoDAO = daoFactory.getRapportoInterventoDAO();
		RapportoIntervento rapportoIntervento;
		try {
			rapportoIntervento = rapportoInterventoDAO.retrieveById(Integer.parseInt(getRequestAttributes().get("id").toString()));
		} catch (NumberFormatException e) {
			return null;
		} catch (SQLException e) {
			return null;
		}
		return rapportoIntervento;
	}

	@Put
	public void update(RapportoIntervento rapportoIntervento) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		RapportoInterventoDAO rapportoInterventoDAO = daoFactory.getRapportoInterventoDAO();
		try {
			rapportoInterventoDAO.update(rapportoIntervento);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Delete
	public void delete(RapportoIntervento rapportoIntervento) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		RapportoInterventoDAO rapportoInterventoDAO = daoFactory.getRapportoInterventoDAO();
		try {
			rapportoInterventoDAO.delete(Integer.parseInt(getRequestAttributes().get("id").toString()));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
